﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Globalization;
using System.IO;
public partial class Main_Reports_Inventory_MPNReport : System.Web.UI.Page
{
    Datautility DU = new Datautility(); 
    DataTable Dt = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Getdata();
        }
    }
    protected void Getdata()
    {
        SqlParameter[] Param = { new SqlParameter("@fromdate",Convert.ToDateTime(txtfromdate.Text).ToString(string.Format("yyyy/MM/dd"))),
                                   new SqlParameter("@todate",Convert.ToDateTime(txttodate.Text).ToString(string.Format("yyyy/MM/dd")))};
        Dt = DU.getdatausingprocedure("IVR_RPT_MPN_REPORT", Param);
        if (Dt.Rows.Count > 0)
        {
            grdMPNView.DataSource = Dt;
            grdMPNView.Columns[13].FooterText = Dt.AsEnumerable().Select(x => x.Field<decimal>("OPENINGQTY")).Sum().ToString("N0");
            grdMPNView.Columns[14].FooterText = Dt.AsEnumerable().Select(x => x.Field<decimal>("PURCHASEQTY")).Sum().ToString("N0");
            grdMPNView.Columns[15].FooterText = Dt.AsEnumerable().Select(x => x.Field<int>("ISSUEQTY")).Sum().ToString("N0");
            grdMPNView.Columns[16].FooterText = Dt.AsEnumerable().Select(x => x.Field<int>("RETURNQTY")).Sum().ToString("N0");
            grdMPNView.Columns[17].FooterText = Dt.AsEnumerable().Select(x => x.Field<decimal>("CLOSINGQTY")).Sum().ToString("N0");
            grdMPNView.DataBind();
        }
        lblRecordNo.Text = grdMPNView.Rows.Count.ToString() + " Record(s)";
        ddlColumnname.Style["display"] = "block";        
        lblvalue.Style["display"] = "block";
        txtValue.Style["display"] = "block";        
        imgbtnPrint.Style["display"] = "block";
        ImgBtnExcel.Style["display"] = "block";
    }
    protected void grdMPNView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdMPNView.PageIndex = e.NewPageIndex;
        Getdata();
    }
    protected void btnReport_Click(object sender, EventArgs e)
    {
        Getdata();
        string Filter = "";
        if (ddlColumnname.SelectedValue == "1")//for Bin No
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "BinNo like'%" + txtValue.Text + "%'");
            grdMPNView.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "2")//for Item Code
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "ItemCode Like'%" + txtValue.Text + "%'");
            grdMPNView.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "3")//For MPN
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "MPN Like'%" + txtValue.Text + "%'");
            grdMPNView.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "4")//For Item group
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "ItemGroup like'%" + txtValue.Text + "%'");
            grdMPNView.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "5")//For Item Name
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "ItemName like'%" + txtValue.Text + "%'");
            grdMPNView.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "6")//For Manufacturer Name
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "ManufacName='" + txtValue.Text + "'");
            grdMPNView.AllowPaging = false;
        }
        else
        {
            Getdata();
        }

        DataRow[] Dr = Dt.Select(Filter);
        DataTable Tempdt = new DataTable();
        Tempdt = Dt.Clone();
        foreach (DataRow drNew in Dr)
        {
            Tempdt.ImportRow(drNew);
        }
        grdMPNView.DataSource = Tempdt;
        grdMPNView.DataBind();
    }
    protected void btngetreport_Click(object sender, EventArgs e)
    {
        Getdata();
    }
    protected void grdMPNView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{
        //    Label lblPurchaseQty = (Label)e.Row.FindControl("lblPurchaseQty");
        //    if (lblPurchaseQty.Text != "0.0")
        //    {
        //        //e.Row.BackColor = System.Drawing.Color.Red;
        //    }
        //}
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm(control);
    }
    protected void ImgBtnExcel_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "MPN_Report.xls"));
            Response.ContentType = "application/ms-excel";
            grdMPNView.ShowFooter = false;
            grdMPNView.AllowPaging = false;
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            grdMPNView.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();
        }
        catch (Exception ex)
        {
        }
    }
}